import axios from 'axios'

const axiosInstance = axios.create({
    timeout: 6000 // 请求超时时间
})

// request interceptor
axiosInstance.interceptors.request.use(config => {
    const accessToken = window.localStorage.getItem('accessToken');
    if (accessToken) {
        config.headers['Authorization'] = accessToken;
        //config.headers[ 'X-Access-Token' ] = accessToken // 让每个请求携带自定义 token 请根据实际情况自行修改
    }

    return config
},(error) => {
    return Promise.reject(error)
})

axiosInstance.interceptors.response.use(
    (response) => {
        const { code, status, data, message, msg } = response;
        if (response && (code === 200 || status === 200)) {
            return response.data;
        } else {
            // $$.removeStore('Token');
            // window.location.href = '/sign/login';
            throw new Error(msg || message || 'error');
        }
    },
    (error) =>
        Promise.reject(
            (error.response && error.response.data) || 'Something went wrong!'
        )
)

export default axiosInstance
